package cn.fscode.code.generator.assembly;

import cn.fscode.code.generator.gen.TableIndexDefinition;
import cn.fscode.code.generator.model.ColumnUnit;
import cn.fscode.code.generator.model.TableColumn;
import cn.fscode.code.generator.model.po.DatasourceConfigPO;
import cn.fscode.code.generator.model.po.GenTableColumnPO;
import cn.fscode.code.generator.model.po.GenTableIndexPO;
import cn.fscode.code.generator.model.po.GenTablePO;
import cn.fscode.code.generator.model.req.GenTableIndexReq;
import cn.fscode.code.generator.model.resp.GenInfoResp;
import cn.fscode.code.generator.model.resp.GenTableColumnResp;
import cn.fscode.code.generator.model.resp.GenTableIndexResp;
import cn.fscode.code.generator.model.resp.GenTableResp;
import cn.fscode.code.generator.template.TmplColumnDefinition;
import cn.fscode.code.generator.template.TmplCsharpColumnDefinition;
import cn.fscode.code.generator.template.TmplJavaColumnDefinition;
import cn.fscode.code.generator.template.TmplTableIndexDefinition;
import cn.fscode.commons.tool.core.StringUtils;
import org.mapstruct.*;

import java.util.List;

/**
 * @author shenguangyang
 */
@Mapper(componentModel = "spring", nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
public interface GenTableAssembly {
    GenTableColumnPO from(TableColumn tableColumn);
    GenTableResp toGenTableRespFrom(GenTablePO genTablePO);

    void updateGenTableColumnPO(ColumnUnit columnUnit, @MappingTarget GenTableColumnPO genTableColumnPO);

    TmplJavaColumnDefinition toTmplJavaColumnDefinition(TmplColumnDefinition tmplColumnDefinition);

    TmplCsharpColumnDefinition toTmplCsharpColumnDefinition(TmplColumnDefinition tmplColumnDefinition);

    @Mappings({
            @Mapping(source = "columnComment", target = "comment"),
            @Mapping(source = "columnLength", target = "length"),
            @Mapping(source = "columnScale", target = "scale"),
            @Mapping(source = "columnType", target = "type"),
    })
    TmplColumnDefinition toTmplColumnDefinition(GenTableColumnPO genTableColumnPO);

    GenTableColumnResp toGenTableColumnResp(GenTableColumnPO genTableColumnPO);

    GenTableIndexResp toGenTableIndexResp(GenTableIndexPO genTableIndexPO);

    GenTableIndexPO toGenTableIndex(GenTableIndexReq genTableIndexReq);

    void updateGenInfoResp(GenTablePO genTablePO, @MappingTarget GenInfoResp genInfoResp);

    void updateGenInfoResp(DatasourceConfigPO datasourceConfigPO, @MappingTarget GenInfoResp genInfoResp);

    @Mappings({
            @Mapping(source = "columnNames", target = "columnNames", qualifiedByName = "toList")
    })
    TmplTableIndexDefinition toTmplTableIndexDefinition(GenTableIndexPO genTableIndexPO);

    @Named("toList")
    default List<String> toList(String str) {
        return StringUtils.toList(str, ",", String.class);
    }

    GenTableIndexPO toGenTableIndex(TableIndexDefinition item);
}
